/*
 * Copyright 2001-2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.training.killerapp;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;

import com.training.killerapp.model.Page;
import com.training.killerapp.model.User;

/**
 * The default implementation of the Killer App.
 * 
 * XXX: Filled with PMD triggers
 * 
 * @since 1.0
 */
public class DefaultKillerApp
    implements KillerApp
{    
    /** Number of times to loop in test. */
    private static final int COUNT = 10;

    /** Test variable. */
    private String unused;

    private KillerAppDataStore datastore;

    /**
     * Creates a new User and Page in the Killer App.
     * 
     * @param email the unique email for the User
     * @param password the user's password
     * @param name the user's display name
     * 
     * @throws SecurityException
     */
    public User createAccount( String email, String password, String name )
    {
        User user = datastore.getUser( email );

        if( user != null )
        {
            throw new SecurityException( "User already exists" );
        }

        user = new User();
        user.setEmail( email );
        user.setPassword( password );
        user.setName( name );

        Page page = new Page();
        page.setName( name );
        page.setOwner( user );
        page.setContents( "This is where I write about myself!" );

        int pageId = datastore.createPage( page, email );
        page.setId( pageId );

        datastore.createUser( user );

        return user;
    }

    /**
     * Retrieves an existing User in the Killer App.
     * 
     * @param email the key of the User to get
     */
    public User getUser( String email )
    {
        return datastore.getUser( email );
    }

    /**
     * Adds friend to an existing User in the Killer App.
     * 
     * @param user the User entry to add friend to - email must be set
     * @param friendEmail the User entry friend - email must be set
     */
    public void addFriend( User user, String friendEmail )
    {
        user = getUser( user.getEmail() );
        User friend = getUser( friendEmail );

        user.addFriend( friend );

        datastore.updateUser( user );
    }

    public Iterator getUserIterator()
    {
        return datastore.getUserCollection().iterator();
    }

    /**
     * Updates a page in the Killer App.
     * 
     * @param owner the owner of the page to manage
     * @param name the name to set for the managed page
     * @param contents the content to set for the managed page
     * 
     * @return the updated Page
     */
    public Page managePage( User owner, String name, String contents )
    {
        Page page = getPage( owner.getEmail() );

        page.setName( name );

        page.setContents( contents );

        datastore.updatePage( page );

        return page;
    }

    /**
     * Retrieves a User's page in the Killer App.
     * 
     * @param ownerEmail the owner of the page to retrieve's email
     */
    public Page getPage( String ownerEmail )
    {
        User user = datastore.getUser( ownerEmail );

        return user == null ? null : user.getPage();
    }

    public void removeUserAndPage( User user )
    {
        datastore.deleteUser( user );
    }

    // No Javadoc.
    private void testMethod()
    {
    }

    /**
     * Test.
     */
    public void testMethod1()
    {
        // This copy and pasted code is intended to be detected by PMD's CPD report
        PrintWriter out = new PrintWriter( new StringWriter() );

        int i;
        for ( i = 0; i < COUNT; i ++ )
        {
            out.println( "i = " + i );
            int j;
            for ( j = 0; j < COUNT; j ++ )
            {
                out.println( "j = " + j );
                int k;
                for ( k = 0; k < COUNT; k ++ )
                {
                    out.println( "k = " + k );
                }
            }
        }
        for ( i = 0; i < COUNT; i ++ )
        {
            out.println( "i = " + i );
            int j;
            for ( j = 0; j < COUNT; j ++ )
            {
                out.println( "j = " + j );
                int k;
                for ( k = 0; k < COUNT; k ++ )
                {
                    out.println( "k = " + k );
                }
            }
        }
    }

    /**
     * Test.
     */
    public void testMethod2()
    {
        // This copy and pasted code is intended to be detected by PMD's CPD report
        PrintWriter out = new PrintWriter( new StringWriter() );

        int i;
        for ( i = 0; i < COUNT; i ++ )
        {
            out.println( "i = " + i );
            int j;
            for ( j = 0; j < COUNT; j ++ )
            {
                out.println( "j = " + j );
                int k;
                for ( k = 0; k < COUNT; k ++ )
                {
                    out.println( "k = " + k );
                }
            }
        }
        for ( i = 0; i < COUNT; i ++ )
        {
            out.println( "i = " + i );
            int j;
            for ( j = 0; j < COUNT; j ++ )
            {
                out.println( "j = " + j );
                int k;
                for ( k = 0; k < COUNT; k ++ )
                {
                    out.println( "k = " + k );
                }
            }
        }
    }
}
